// src/stores/product.js
import {defineStore} from 'pinia'
import {productApi} from '@/api/product.js'

export const useProductStore = defineStore('product', {
    state: () => ({
        products: [],
        total: 0,
        loading: false,
        currentProduct: null
    }),

    actions: {
        async fetchProducts(params = {}) {
            this.loading = true
            try {
                const response = await productApi.getProducts(params)
                this.products = response.data.records || []
                this.total = response.data.total || 0
            } finally {
                this.loading = false
            }
        },

        async updateContent(data) {
            this.loading = true
            try {
                return await productApi.updateContent(data)
            } finally {
                this.loading = false
            }
        },

        async fetchProduct(id) {
            this.loading = true
            try {
                const response = await productApi.getProduct(id)
                this.currentProduct = response.data
                return response.data
            } finally {
                this.loading = false
            }
        },

        async crawlProducts(params) {
            return await productApi.crawlProducts(params)
        },

        async fetchProductDetail(id) {
            return await productApi.fetchDetail(id)
        },

        async deleteProduct(id) {
            return await productApi.deleteProduct(id)
        }
    }
})